package com.chonor.mall.controller;

import com.chonor.mall.common.CommonPage;
import com.chonor.mall.common.CommonResult;
import com.chonor.mall.model.Role;
import com.chonor.mall.service.MemberService;
import com.chonor.mall.vo.MemberDetailVO;
import com.chonor.mall.vo.MemberListVO;
import com.chonor.mall.vo.MemberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "MemberController", description = "会员管理")
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation("根据角色名称分页获取角色列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<MemberVO>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<MemberVO> memberList = memberService.getMemberList(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(memberList));
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @RequestMapping(value = "/getItem/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MemberVO> getItem(@PathVariable  Long id) {
        MemberVO item = memberService.getItem(id);
        return CommonResult.success(item);
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(@PathVariable  Long id,@RequestParam(value = "status") Integer status) {
        int count = memberService.updateStatus(id, status);
        if(count > 0){
            return CommonResult.success(count);
        }
        return CommonResult.failed("更新会员状态失败");
    }
}
